﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Component.Worker.RecursiveProcess;
using CMS.BLL;
using CMS.DAL;

public partial class AdminX_StaticPages_StaticPageMain : System.Web.UI.UserControl
{

    #region Define Var
    protected const string VERTICAL_COLUMN_NAME = "VerticalSort";
    protected const string HOLIZOTAL_COLUMN_NAME = "HolizotalSort";
    private DataTable mCateData = new DataTable();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            this.ShowMess();
            this.BindDataToLanguageDropDown();
            BindData(1);
        }
    }

    #region Bindatato DropDown

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
        ddlLanguage.SelectedValue = ConvertData.ConvertToString(Support.GetDefaultLanguage());
    }

    protected string CreateMarginLeft(object iHolizoltal, int pID)
    {
        int iParentID = 1;
        int marginLeft = 0;
        if (!Convert.IsDBNull(iHolizoltal))
        {
            int iHolzl = Convert.ToInt16(iHolizoltal);
            marginLeft = iHolzl * 80;
        }
        if (marginLeft == 0)
        {
            iParentID = pID;
        }
        string fontBold = (marginLeft == 0) ? "font-weight:bold;" : String.Empty;
        return "margin-left:" + marginLeft + "px;" + fontBold;
    }

    #endregion

    #region Funtion

    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
    }

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }

    private void BindData(int CurrentPageIndex)
    {

        int nDefaultLanguage = Support.GetDefaultLanguage();
        int nStatus = -1;
        string sKeyword ="";
        int nLanguageID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nPageSize = Config.PAGESIZE;
        int nPageCount = Config.PAGECOUNT;

        DataTable dtbPage = new DataTable();
        StaticPages objPage = new StaticPages();

        dtbPage = objPage.Search(sKeyword, 0, nDefaultLanguage, nLanguageID, nStatus, CurrentPageIndex, nPageSize, ref nPageCount);
        FlowCategory flow = new FlowCategory(dtbPage, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);

        if (nStatus == -1 && sKeyword == string.Empty) // search all
        {
            DataTable objTable = new DataTable();
            StaticPages obj = new StaticPages();
            objTable = obj.Search("", 0, nDefaultLanguage, nLanguageID, -1, 1, 10000, ref nPageCount);

            FlowCategory objflow = new FlowCategory(objTable, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);
            objflow.IDColumnName = "StaticPageID";
            objflow.ParentIDColumName = "[Parent]";
            switch (nLanguageID)
            {
                case 1:
                    flow.SortColumnName = "MenuName1";
                    break;
                case 2:
                    flow.SortColumnName = "MenuName2";
                    break;
                case 3:
                    flow.SortColumnName = "MenuName3";
                    break;
            }
            objflow.SortColumnName = "Priority";
            objflow.ParentSortDirection = "ASC";

            if (objflow.Execute(0))
            {
                int iCurrIndex = 0;
                if (CurrentPageIndex > 0) iCurrIndex = CurrentPageIndex - 1;
                mCateData = objflow.DestinationData();
                FlowPager pager = new FlowPager(mCateData, nPageSize, "[Parent]");

                DataView vwPage = pager.GeneralPageByIndex(iCurrIndex, VERTICAL_COLUMN_NAME);
                hidSearchStatus.Value = "1";
                RpStaticPage.DataSource = vwPage;


                nPageCount = mCateData.Rows.Count;
                nPageSize = pager.TotalRecord;
            }

            mCateData = objflow.DestinationData();

            RpStaticPage.DataBind();
            lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + " " + +nPageCount + " " + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];


        }
        else // Search Active or Inactive
        {
            hidSearchStatus.Value = "0";
            RpStaticPage.DataBind();
            lblTotalRecord.Text = ConvertData.ConvertToString(nPageCount);
        }
    }

    #endregion

    #region Repeater processing

    protected void RpStaticPage_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;

        string strCommand = e.CommandName;
        int IntpageID = ConvertData.ConvertToInt(e.CommandArgument);

        StaticPages objPage = new StaticPages();
        int nStatus = 0;
        switch (strCommand)
        {
            case "Edit":
                Session[Config.SESSION_LANGID] = ConvertData.ConvertToString(Support.GetDefaultLanguage());
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_STATICPAGE_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + IntpageID;
                Response.Redirect(sEdit);
                break;

            case "Delete":
                objPage.LoadById(IntpageID);
                string sDelImage = Support.DeleteFile("staticpage", ConvertData.ConvertToString(objPage.Data.Images));
                nStatus = objPage.DeleteById(IntpageID);

                BindData(1);
                break;

            case "Active":
                nStatus = objPage.UpdateStatus(IntpageID, Config.STATUS_INACTIVE);

                BindData(1);
                break;
            case "Inactive":
                nStatus = objPage.UpdateStatus(IntpageID, Config.STATUS_ACTIVE);

                BindData(1);
                break;
        }
    }

    protected void RpStaticPage_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();

        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            int intStatusCheck = ConvertData.ConvertToInt(hidSearchStatus.Value);
            int intParent = 0;

            Label lblName = (Label)e.Item.FindControl("lblName");
            Label lblPageCode = (Label)e.Item.FindControl("lblPageCode");
            Label lbl2ndName = (Label)e.Item.FindControl("lbl2ndName");
            Label lblDateModifined = (Label)e.Item.FindControl("lblDateModifined");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");

            intParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Parent"));
            if (intStatusCheck > 0) // is Search All
            {
                lblName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MenuName"));
                lbl2ndName.Visible = false;
                lblName.Visible = true;
            }
            else
            {

                lbl2ndName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MenuName"));
                lbl2ndName.Visible = true;
                lblName.Visible = false;
                if (intParent > 0) // is Child
                    lbl2ndName.CssClass = "MarginLeft";

            }
            int RootParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "StaticPageID"));

            lblDateModifined.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "DateCreated")));
            lblPageCode.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "PageCode"));
            int intPageID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "StaticPageID"));
            //  lnkEdit.NavigateUrl = strSlash + EditPage(intPageID);
            int intStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            if (intStatus > 0) // Active
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }
        }

        if (e.Item.ItemType != ListItemType.Footer && e.Item.ItemType != ListItemType.Header)
        {
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            int page_ID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "StaticPageID"));
            int nParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Parent"));
            lnkActive.Text = MultipleLanguage.STATUS_ACTIVE[nDefaultLanguage - 1];
            lnkInactive.Text = MultipleLanguage.STATUS_INACTIVE[nDefaultLanguage - 1];


            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StaticPageID"));

            if (nParent > 0)
            {
                lnkDelete.Visible = true;

            }
            else
            {
                lnkDelete.Visible = false;


            }
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StaticPageID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_CATEGORY_CONFIRM[nDefaultLanguage - 1]);

            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StaticPageID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage]);
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StaticPageID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLanguage - 1]);

        }
    }

    #endregion

    #region Events Click

    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_STATICPAGE_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindData(1);
    }

    #endregion
    

    

    

}